package com.ms.content.service.impl;

import com.alibaba.nacos.common.util.Md5Utils;
import com.alibaba.nacos.common.util.UuidUtils;
import com.ms.content.dao.AdmsDao;
import com.ms.content.entity.AdmsEntity;
import com.ms.content.util.MD5Util;
import com.ms.content.vo.MaVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ms.common.utils.PageUtils;
import com.ms.common.utils.Query;

import com.ms.content.dao.MiaoshaActivityDao;
import com.ms.content.entity.MiaoshaActivityEntity;
import com.ms.content.service.MiaoshaActivityService;
import org.springframework.transaction.annotation.Transactional;


@Service("miaoshaActivityService")
public class MiaoshaActivityServiceImpl extends ServiceImpl<MiaoshaActivityDao, MiaoshaActivityEntity> implements MiaoshaActivityService {

    @Autowired
    private AdmsDao admsDao;

    @Autowired
    private StringRedisTemplate stringredisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MiaoshaActivityEntity> page = this.page(
                new Query<MiaoshaActivityEntity>().getPage(params),
                new QueryWrapper<MiaoshaActivityEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void upflag(Integer id) {
        QueryWrapper<MiaoshaActivityEntity> wrapper = new QueryWrapper<MiaoshaActivityEntity>().eq("id", id);
        MiaoshaActivityEntity miaoshaActivityEntity = baseMapper.selectOne(wrapper);
        miaoshaActivityEntity.setFlag(1);
        baseMapper.updateById(miaoshaActivityEntity);
    }

    @Override
    public void inssave(MaVo maVo) {
        MiaoshaActivityEntity entity = new MiaoshaActivityEntity(); //秒杀信息表
        AdmsEntity admsEntity = new AdmsEntity(); //中间表
        BeanUtils.copyProperties(maVo,entity);
        BeanUtils.copyProperties(maVo,admsEntity);
        System.out.println(entity);
        baseMapper.insert(entity);
        admsEntity.setMid(Math.toIntExact(entity.getId()));
        //将库存存入到redis中
        stringRedisTemplate.opsForValue().set(admsEntity.getId()+"",admsEntity.getMsCount()+"");
        admsDao.insert(admsEntity);
    }

    /**
     * 创建一个临时的秒杀地址
     * @param msid
     * @return
     */
    @Override
    @Transactional
    public String createMiaoshaPath(Integer msid,String uid) {
//        if (msid.equals(0)){
//            return null;
//        }
        String s = MD5Util.md5(UuidUtils.generateUuid() + "123456");
            stringredisTemplate.opsForValue().set("miaosha"+msid,s,1, TimeUnit.SECONDS);
            System.out.println("生成地址: "+s);
            String s2 = stringredisTemplate.opsForValue().get(uid + "uid");
            System.out.println(s2);
            if (StringUtils.isEmpty(s2)){
                stringredisTemplate.opsForValue().set(uid + "uid",5+"",1,TimeUnit.SECONDS);
            }else{
                stringredisTemplate.opsForValue().decrement(uid+"uid",1);
                Integer s1 = Integer.valueOf(stringredisTemplate.opsForValue().get(uid + "uid"));
                System.out.println(s1);
                if (s1<=0){
                    return null;
                }
            }
        return s;
    }

}